package com.haierp.util.ExcelTool;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.haierp.model.ErpOrder;
import com.haierp.service.IErpOrderService;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.ErpOrderQueryVO;

public class ExcelErpOrderExport {

	private String[] columnTitles = new String[]{"id","outerOrderId","outerDetailOrderId","targetNo","orderNo","erpNo","status","stockStatus","upc","skuId","skuCode","itemId","itemName","color","scale","freight","salePrice","quantity","shippingOrderId","gmtCreate","gmtModify","userCreate","userModify","receiver","addressDetail","telephone","postcode","remark","warehouseName","receiverState","receiverCity","receiverDistrict","idCard","purchasePrice"};
	private IErpOrderService erpOrderService;
	
	public ExcelErpOrderExport(IErpOrderService erpOrderService) {
		// TODO Auto-generated constructor stub
		this.erpOrderService = erpOrderService;
	}

	public Boolean exportOrder(String name) throws Exception {

		URL url = ExcelHelper.class.getClassLoader().getResource(name);
		if (null == url) {
			
//			throw new Exception("File not found: " + name);
			return false;
		}
		
		return exportOrder(url);
	}
	
	public boolean exportOrder(URL url) throws Exception {

		if (null == url) {
			
//			throw new Exception("File not found: " + name);
			return false;
		}
		
		File file = new File(url.getPath());		
		String filepath = file.getAbsolutePath();		
		if (null == filepath) {
//			throw new Exception("File not found: " + name);
			return false;
		}

		ExcelHelper excelHelper = new ExcelHelper();
		try {			
			List<List<Object>> rowDatas = getErpOrderRowDatas();
			
			excelHelper.setToSheet("Erp Order", columnTitles, rowDatas);
			
			excelHelper.writeToFile(filepath);			
		} catch (Exception e) {
			throw e;
		} finally {
			excelHelper.close();
		}
		
		return true;
	}
	
	private List<List<Object>> getErpOrderRowDatas() {
		List<List<Object>> rowDatas = new ArrayList<>();
		
		try {
			JsonPageResult<List<ErpOrder>> result = new JsonPageResult<>();
			ErpOrderQueryVO erpOrderQueryVO = new ErpOrderQueryVO();
			result = erpOrderService.queryErpOrders(erpOrderQueryVO);
			
			if (null != result.getData()) {
				for (ErpOrder erpOrder : result.getData()) {
					List<Object> list = new ArrayList<>();
					list.add(erpOrder.getId());
					list.add(erpOrder.getOuterOrderId());
					list.add(erpOrder.getOuterDetailOrderId());
					list.add(erpOrder.getTargetNo());
					list.add(erpOrder.getOrderNo());
					list.add(erpOrder.getErpNo());
					list.add(erpOrder.getStatus());
					list.add(erpOrder.getStockStatus());
					list.add(erpOrder.getUpc());
					list.add(erpOrder.getSkuId());
					list.add(erpOrder.getSkuCode());
					list.add(erpOrder.getItemId());
					list.add(erpOrder.getItemName());
					list.add(erpOrder.getColor());
					list.add(erpOrder.getScale());
					list.add(erpOrder.getFreight());
					list.add(erpOrder.getSalePrice());
					list.add(erpOrder.getQuantity());
					list.add(erpOrder.getShippingOrderId());
					list.add(erpOrder.getGmtCreate());
					list.add(erpOrder.getGmtModify());
					list.add(erpOrder.getUserCreate());
					list.add(erpOrder.getUserModify());
					list.add(erpOrder.getReceiver());
					list.add(erpOrder.getAddressDetail());
					list.add(erpOrder.getTelephone());
					list.add(erpOrder.getPostcode());
					list.add(erpOrder.getRemark());
					list.add(erpOrder.getWarehouseName());
					list.add(erpOrder.getReceiverState());
					list.add(erpOrder.getReceiverCity());
					list.add(erpOrder.getReceiverDistrict());
					list.add(erpOrder.getIdCard());
					list.add(erpOrder.getPurchasePrice());
					
					rowDatas.add(list);
				}
			}			
		} catch (Exception e) {
			// TODO: handle exception
			rowDatas = new ArrayList<>();
		}
		
		return rowDatas;
	}
}
